package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientCaseFileService;
import com.naiterui.ehp.bs.patient.service.IPatientDoctorService;
import com.naiterui.ehp.bs.patient.vo.casefile.CaseDetailVO;
import com.naiterui.ehp.bs.patient.vo.casefile.CaseListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 提供给医生服务的远程接口控制器
 * <P>File name : DoctorRemoteController.java </P>
 * <P>Author : NTR.Amber </P>
 * <P>Date : 2015年9月13日 </P>
 */
@Api(value = "病例档案相关", tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.PATIENT_MANAGER})
@RequestMapping("ap/remote/doctor")
@Controller
public class ApDoctorRemoteController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApDoctorRemoteController.class);

    @Autowired
    private IPatientCaseFileService patientCaseFileService;

    @Autowired
    private IPatientDoctorService patientDoctorService;

    /**
     * 分页获取患者病历档案
     * PatientCaseController.getCaseFileList()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月13日 </P>
     *
     * @param patientId
     * @param pageParamVO
     *
     * @return
     */
    @RequestMapping("case/list")
    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageVO<CaseListVO> getCaseFileList(Long doctorId, Long patientId, PageParamVO pageParamVO) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        // 参数错误，记录日志
        if (patientId == null || doctorId == null) {
            LOGGER.error("case list ERROR ; illegal param : doctorId {}, patientId {}, page {}, num{}", doctorId, patientId,
                         pageParamVO.getPage(), pageParamVO.getNum());
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        // 不存在私人医生关系，不允许查看患者病历档案
        if (!this.patientDoctorService.getPersonalRelation(patientId, doctorId)) {
            LOGGER.error("case list ERROR ; doctor and patient mismatching : doctorId {},patientId {},page {},num{}", doctorId, patientId,
                         pageParamVO.getPage(), pageParamVO.getNum());
            throw new BusinessException(ExceptionCodes.ILLEGAL_REQUEST);
        }
        // 分页获取结果集
        return this.patientCaseFileService.getCaseFileList(patientId, pageParamVO.getPage(), pageParamVO.getNum());
    }

    /**
     * 查看患者某病例档案详情。
     * DoctorRemoteController.getCaseFileDetail()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月13日 </P>
     *
     * @param patientId
     * @param caseId
     *
     * @return
     */
    @RequestMapping("case/detail")
    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public CaseDetailVO getCaseFileDetail(Long doctorId, Long patientId, String caseId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        // 参数异常
        if (patientId == null || doctorId == null || StringUtils.isBlank(caseId)) {
            LOGGER.error("case detail ERROR ; illegal param : patientId {}, doctorId {}, caseId {}", patientId, doctorId, caseId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        // 不存在私人医生关系，不允许查看患者病历档案
        if (!this.patientDoctorService.getPersonalRelation(patientId, doctorId)) {
            LOGGER.error("case detail ERROR ;  doctor and patient mismatching : doctorId {}, patientId {}", doctorId, patientId);
            throw new BusinessException(ExceptionCodes.ILLEGAL_REQUEST);
        }
        return this.patientCaseFileService.getCaseDetail(patientId, caseId);
    }
}
